<?php


namespace App\Common\Utils;


use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;

final class ResponseUtils
{
    public static function success($data)
    {
        $response = new JsonResponse([
            'success' => true,
            'code' => 0,
            'message' => "请求成功",
            'data' => $data
        ]);
        $response->headers->set("Content-Type", "application/json;charset=UTF-8");
        $response->setEncodingOptions(JSON_UNESCAPED_UNICODE);

        return $response;
    }

    public static function error($code, $msg, $status = Response::HTTP_INTERNAL_SERVER_ERROR)
    {

        $response = new JsonResponse([
            'success' => false,
            'code' => $code,
            'message' => $msg
        ]);
        $response->headers->set("Content-Type", "application/json;charset=UTF-8");
        $response->setEncodingOptions(JSON_UNESCAPED_UNICODE);
        $response->setStatusCode($status);

        return $response;
    }
}